package Fisica.Compra;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import Fisica.Conexion;
import Inteface.InterfaceFisica;
import Logica.Util;
import Modelo.Compra;
import Modelo.Toner;

public class DAOCompra implements InterfaceFisica{
	public static String SELECT = "SELECT * FROM `compra` WHERE `proveedor` = ? and `fecha` = ?";
	
	public static String FIRST = "SELECT * FROM `toner` order by `id` asc";
	public static String LAST = "SELECT * FROM `toner` order by `id` desc";
	public static String PRE = "SELECT * FROM `toner` WHERE `id` < ? order by `id` desc";
	public static String POST = "SELECT * FROM `toner` WHERE `id` > ? order by `id` asc";
	
	public boolean insert(Object obj) { //ok
		Compra v = (Compra)obj;
		
		//INSERT INTO `Compra` (`marca`, `modelo`, `codigo`) VALUES ("HP", "A12","A1234");
		String INSERTAR = "INSERT INTO `Compra` (`proveedor`, `fecha`) "
		+ "VALUES (?, ?);";


		try {			
			PreparedStatement st;
			ResultSet rs;
			//INSERTO
			st = Conexion.getConexion().prepareStatement(INSERTAR);	
			st.setString(1, v.getProveedor().toUpperCase());
			st.setString(2, Util.getDate(v.getFecha()));
			
			st.executeUpdate();
			//RECUPERO
			st = Conexion.getConexion().prepareStatement(SELECT);
			st.setString(1, v.getProveedor().toUpperCase());
			st.setString(2, Util.getDate(v.getFecha()));
			
			rs = st.executeQuery();
			if(rs.next())
				v.setId(rs.getInt("id"));
			System.out.println("inserto: "+v.getId());

			return true;
			
		}catch(Exception e){
			System.out.println("exception al intentar insertar: "+v.getId());
			e.printStackTrace();
		}
		return false;
			
	}
	
	public boolean update(Object obj){ //ok
		Compra v = (Compra)obj;
		
		String UPDATE = "UPDATE `Compra` SET `proveedor` = ?, `fecha` = ? "
		+ "WHERE `id` = ?;";

		try {			
			PreparedStatement st;
			ResultSet rs;
			//INSERTO
			st = Conexion.getConexion().prepareStatement(UPDATE);	
			st.setString(1, v.getProveedor().toUpperCase());
			st.setString(2, Util.getDate(v.getFecha()));
			st.setInt(3, v.getId());
			
			st.executeUpdate();
			//RECUPERO
			st = Conexion.getConexion().prepareStatement(SELECT);
			st.setString(1, v.getProveedor().toUpperCase());
			st.setString(2, Util.getDate(v.getFecha()));
			
			rs = st.executeQuery();
			if(rs.next())
				v.setId(rs.getInt("id"));
			System.out.println("actualizo: "+v.getId());

			return true;
			
		}catch(Exception e){
			System.out.println("exception al intentar insertar: "+v.getId());
			e.printStackTrace();
		}
		return false;
			
	}
	
	
	public boolean delete(Object obj){ //ok
		Compra v = (Compra)obj;
		String delete = "delete from `Compra` where `id` = ?;";
		String select = "select * from `Compra` where `id` = ?;";
		try {			
			PreparedStatement st;
			ResultSet rs;
			st = Conexion.getConexion().prepareStatement(delete);
			st.setInt(1, v.getId());
			st.executeUpdate();
			
			st = Conexion.getConexion().prepareStatement(select);	
			st.setInt(1, v.getId());
			rs = st.executeQuery();
			
			System.out.println("inserto: "+v.getId());

			return !rs.next();
			
		}catch(Exception e){
			System.out.println("exception al intentar insertar: "+v.getId());
			e.printStackTrace();
		}
		return false;
	}
	
	public List<Object> list (){ //ok
		List<Object> lista = new ArrayList();	
		String sql = "select * from `Compra`;";
		try {
			PreparedStatement st = Conexion.getConexion().prepareStatement(sql);	
			ResultSet rs = st.executeQuery();			
			
			while (rs.next()){
				Compra o = new Compra(); 
				o.setId(rs.getInt("id")); 				
				o.setProveedor(rs.getString("proveedor"));				
				o.setFecha(rs.getDate("fecha"));					
								
				lista.add(o);
				}			
		} catch (SQLException e) {
			System.out.print("ERROR EN LA RECUPERACION DE DATOS DE LA BASE");
			e.printStackTrace();
		} 		
		return lista;		
	}

	public Object getById(int id){ //ok
		String sql = "select * from `Compra` where `id` = "+id+";";
		try {
			PreparedStatement st = Conexion.getConexion().prepareStatement(sql);	
			ResultSet rs = st.executeQuery();			
			
			if (rs.next()){
				Compra o = new Compra(); 
				o.setId(rs.getInt("id")); 				
				o.setProveedor(rs.getString("proveedor"));				
				o.setFecha(rs.getDate("fecha"));			 	
				return o;
				}			
		} catch (SQLException e) {
			System.out.print("ERROR EN LA RECUPERACION DE DATOS DE LA BASE");
			e.printStackTrace();
		} 		
		return new Compra();	
	}

	

	public static void main(String[] args) {
		Compra t = new Compra();
		t.setProveedor("enix");
		t.setFecha(new java.util.Date());
		
		DAOCompra daot = new DAOCompra();
//		daot.insert(t); //ok
		Compra t2 = (Compra) daot.getById(1); //ok
		System.out.println("marca: "+t2.getProveedor());
//		t2.setProveedor("epson");
//		if (daot.update(t2)) //ok
//			System.out.println("EXITO");
//		else
//			System.out.println("FRACASO");
		
//		List<Object> l = daot.list(); // ok
//		Compra y;
//		for(Object x: l){
//			y = (Compra)x;
//			System.out.println("id: "+y.getId());
//			System.out.println("prov: "+y.getProveedor());
//			System.out.println("fecha: "+y.getFecha());
//			
//		}
		
//		System.out.print("DELETE");
//		if (daot.delete(t2)) //ok
//			System.out.println("EXITO");
//		else
//			System.out.println("FRACASO");
//		
		
	}

	public Object getObject (PreparedStatement st){ //ok
		Compra o = new Compra(); 	
		
		try {
//			PreparedStatement st = Conexion.getConexion().prepareStatement(sql);	
			ResultSet rs = st.executeQuery();			
			
			if (rs.next()){
				o.setId(rs.getInt("id")); 				
				o.setProveedor(rs.getString("proveedor"));				
				o.setFecha(rs.getDate("fecha"));				
			}	
			return o;
		} catch (SQLException e) {
			System.out.print("ERROR EN LA RECUPERACION DE DATOS DE LA BASE");
			e.printStackTrace();
		} 		
		return new Toner();		
	}
	
	@Override
	public Object getPre(int id) {
		try {
			PreparedStatement st = Conexion.getConexion().prepareStatement(PRE);
			st.setInt(1, id);
			return getObject(st);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		
		return null;
	}

	@Override
	public Object getPost(int id) {
		try {
			PreparedStatement st = Conexion.getConexion().prepareStatement(POST);
			st.setInt(1, id);
			return getObject(st);
		} catch (SQLException e) {			
			e.printStackTrace();
		}		
		return null;
	}

	@Override
	public Object getFirst() {
		try {
			PreparedStatement st = Conexion.getConexion().prepareStatement(FIRST);
			return getObject(st);
		} catch (SQLException e) {			
			e.printStackTrace();
		}		
		return null;
	}

	@Override
	public Object getLast() {
		try {
			PreparedStatement st = Conexion.getConexion().prepareStatement(LAST);
			return getObject(st);			
		} catch (SQLException e) {			
			e.printStackTrace();
		}		
		return null;
	}

	@Override
	public boolean exist(Object obj) { //ok
		Compra v = (Compra)obj;
		
		try {			
			PreparedStatement st;
			ResultSet rs;

			//RECUPERO
			st = Conexion.getConexion().prepareStatement(SELECT);
			st.setString(1, v.getProveedor());
			st.setString(2, Util.getDate(v.getFecha()));
			
			rs = st.executeQuery();
			if(rs.next())
				v.setId(rs.getInt("id"));
			System.out.println("existe: "+v.getId());

			return true;
			
		}catch(Exception e){
			System.out.println("exception al existir: "+v.getId());
			e.printStackTrace();
		}
		return false;
			
	}

	public List<Object> listProveedores() {
		List<Object> lista = new ArrayList();	
		
		String SELECT = "SELECT * FROM `compra` group by `proveedor`";
		
		try {	
			PreparedStatement st;
			ResultSet rs;
			//RECUPERO
			st = Conexion.getConexion().prepareStatement(SELECT);
						
			rs = st.executeQuery();
			while (rs.next()){
				Compra o = new Compra(); 
				o.setId(rs.getInt("id")); 				
				o.setProveedor(rs.getString("proveedor"));				
				o.setFecha(rs.getDate("fecha"));					
								
				lista.add(o);
				}
			
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return lista;
			
	}
}
